<script setup lang="ts">
import { login, register } from '@/api/login'
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import forget from './components/forget_password.vue'
import { useRouter } from 'vue-router'
import { useUserInfor } from '@/store/userInfo'
// 表单接口
interface FormData {
  account: number | null
  password: string
  nextPassword?: string
}
interface Store {
  imgUrl: string
  name: string
  sex: string
  department: string
  identity: string
  account: number
  email: string
  userInfo: () => Promise<void>
}
// 登录表单数据
const loginData: FormData = reactive({
  account: 111111,
  password: '111111'
})
// 注册表单数据
const registerData: FormData = reactive({
  account: null,
  password: '',
  nextPassword: ''
})

// tabs 标签默认显示
const activeName = ref('first')
const router = useRouter()
// 引入store 实例
const store = useUserInfor()
// 登录
const Login = async () => {
  try {
    const res = await login(loginData)
    const { token } = res.data
    if (res.data.message === '登录成功') {
      ElMessage.success('登录成功')
      // 存储账户id
      localStorage.setItem('id', res.data.results.id)
      // 将账户信息存入store中
      // -------此处代码需要处理--------

      store.imgUrl = res.data.results.imgUrl
      store.name = res.data.results.name
      store.sex = res.data.results.sex
      store.department = res.data.results.department
      store.identity = res.data.results.identity
      store.email = res.data.results.email 
      store.account = res.data.results.account
      // -------暂时不能使用pinia中的Actions中的方法--
      // 清空表单信息
      loginData.account = null
      loginData.password = ''
      // 将数据存入本地
      localStorage.setItem('token', token)
      router.push('/home')
    } else if (res.data.message === '密码错误') {
      ElMessage.error('密码错误')
    } else if (res.data.message === '账号不存在') {
      ElMessage.error('账号未注册')
    } else if (res.data.message === '账号被冻结') {
      ElMessage.error('账号被冻结')
    } else {
      ElMessage.error('登录失败,请检查数据')
    }
  } catch (error) {
    ElMessage.error('登录失败')
  }
}
// 注册
const Register = async () => {
  try {
    if (registerData.password === registerData.nextPassword) {
      const res = await register(registerData)
      if (res.data.message === '注册用户成功') {
        ElMessage.success('注册成功')
        // 转为登录界面
        activeName.value = 'first'
      } else if (res.data.message === '该账号已被注册') {
        ElMessage.error('该账号已被注册')
      } else {
        ElMessage.error('注册失败,请检查数据')
      }
    } else {
      ElMessage.error('两次密码不一致')
    }
  } catch (error) {
    ElMessage.error('注册失败')
  }
}

const forgetPwd = ref()

// 打开忘记密码弹窗
const openForget = () => {
  forgetPwd.value.open()
}
</script>

<template>
  <div class="common-layout">
    <el-container>
      <el-header class="header-wrapped">
        <div class="header-content">
          <h3>通用后台管理系统</h3>
          <span class="welcome">欢迎您的登录</span>
        </div>
      </el-header>
      <el-main>
        <div class="login-wrapped">
          <el-card class="box-card">
            <el-tabs v-model="activeName" stretch>
              <el-tab-pane label="登录" name="first">
                <el-form class="login-form">
                  <el-form-item label="账号">
                    <el-input
                      v-model="loginData.account"
                      placeholder="请输入账号"
                    />
                  </el-form-item>
                  <el-form-item label="密码">
                    <el-input
                      v-model="loginData.password"
                      placeholder="请输入密码"
                      show-password
                    />
                  </el-form-item>
                  <!-- 底部外壳 -->
                  <div class="footer-wrapped">
                    <div class="forget-password">
                      <span class="forget-password-button" @click="openForget"
                        >忘记密码</span
                      >
                    </div>
                    <div class="footer-button">
                      <el-button type="primary" @click="Login">登录</el-button>
                    </div>
                    <div class="footer-go-register">
                      还没有账号？<span
                        class="go-register"
                        @click="activeName = 'second'"
                        >马上注册</span
                      >
                    </div>
                  </div>
                </el-form>
              </el-tab-pane>
              <el-tab-pane label="注册" name="second">
                <el-form class="login-form">
                  <el-form-item label="账号">
                    <el-input
                      v-model="registerData.account"
                      placeholder="账号长度6-12位"
                    />
                  </el-form-item>
                  <el-form-item label="密码">
                    <el-input
                      v-model="registerData.password"
                      placeholder="密码需长度6-12位含字母数字"
                    />
                  </el-form-item>
                  <el-form-item label="确认密码">
                    <el-input
                      v-model="registerData.nextPassword"
                      placeholder="请再次输入密码"
                    />
                  </el-form-item>
                  <div class="footer-button">
                    <el-button type="primary" @click="Register">注册</el-button>
                  </div>
                </el-form>
              </el-tab-pane>
            </el-tabs>
          </el-card>
        </div>
      </el-main>
      <el-footer class="footer-wrapped">
        <div class="footer-content">
          <span>© 2024 版权所有</span>
        </div>
      </el-footer>
    </el-container>
  </div>
  <forget ref="forgetPwd"></forget>
</template>

<style lang="less" scoped>
// 头部外壳
.header-wrapped {
  // 头部内容
  .header-content {
    width: 1200px;
    margin: 0 auto;
    line-height: 60px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    // 欢迎语
    .welcome {
      font-size: 13px;
    }
  }
}

// 主体部分
.el-main {
  background-image: url('@/assets/01background.png');
  height: 600px;
  background-size: cover;
  background-repeat: no-repeat;
  --el-main-padding: 0;

  // 登录外壳
  .login-wrapped {
    width: 1200px;
    height: 600px;
    margin: 0 auto;

    // 卡片样式
    .box-card {
      width: 350px;
      height: 375px;
      float: right;
      position: relative;
      top: 14%;

      .login-form {
        // 登录底部外壳
        .footer-wrapped {
          display: flex;
          flex-direction: column;

          .forget-password {
            display: flex;
            justify-content: flex-end;
            margin: 10px 0;

            .forget-password-button {
              color: #409eff;
              // 鼠标移入的效果
              cursor: pointer;
            }
          }

          .footer-go-register {
            margin: 12px 0;
            display: flex;
            justify-content: flex-end;

            .go-register {
              color: #409eff;
              cursor: pointer;
            }
          }
        }

        // 底部登录按钮
        .footer-button {
          width: 100%;
          display: flex;
          justify-content: center;

          .el-button {
            width: 100%;
          }
        }
      }
    }
  }
}

// 底部外壳
.footer-wrapped {
  margin-top: 10px;

  // 底部内容
  .footer-content {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    justify-content: center;
    align-items: center;

    // 底部介绍
    .title {
      color: #666;
    }

    span {
      color: #666;
    }
  }
}

// 样式穿透部分
// 表单边距
.el-form {
  margin-top: 30px;
}

// tabs标签
:deep(.el-tabs__item) {
  color: #333;
  font-size: 18px;
}

// 输入框高度
:deep(.el-input__inner) {
  height: 40px;
}

// 输入框标签字体高度
:deep(.el-form-item__label) {
  height: 40px;
  line-height: 40px;
}

// 登录按钮
:deep(.el-button) {
  width: 300px;
  height: 45px;
  font-size: 16px;
}
</style>
